package com.grq.mis.service;

import com.grq.mis.dao.NoticeDao;
import com.grq.mis.entity.Notice;
import com.grq.mis.exception.BusinessException;
import com.grq.mis.exception.ResultEnum;
import com.grq.mis.model.sys.Me;
import com.grq.mis.dao.NoticeDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Transactional
@Service
public class NoticeService extends BaseService<Notice> {

    @Autowired
    private NoticeDao noticeDao;

    /**
     * 获取最新的通知
     * @return
     */
    public Notice getLast() {
        return noticeDao.createLambdaQuery().andEq(Notice::getIsDisable,false).desc(Notice::getId).single();
    }

    public void addNotice(Notice notice, Me me) {

        notice.setCreateTime(new Date());
        notice.setCreateUserId(me.getId());
        notice.setCreateUserName(me.getName());
        notice.setIsDisable(false);
        noticeDao.insert(notice);

    }

    public void updateNotice(Notice model) {

        Notice notice = noticeDao.single(model.getId());
        if (notice == null) {
            throw new BusinessException(ResultEnum.NOTICE_IS_NULL);
        }

        notice.setTitle(model.getTitle());
        notice.setContent(model.getContent());
        notice.setIsDisable(model.getIsDisable());
        notice.setIsTip(model.getIsTip());
        noticeDao.updateById(notice);

    }

    public void deleteNotice(Long id) {

        Notice notice = noticeDao.single(id);
        if (notice == null) {
            throw new BusinessException(ResultEnum.NOTICE_IS_NULL);
        }

        noticeDao.deleteById(id);

    }
}
